package com.veer.shop.service.impl.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.veer.shop.config.BizStatusEnum;
import com.veer.shop.config.Constants;
import com.veer.shop.config.ResponseCodeEnum;
import com.veer.shop.mapper.AdminUserMapper;
import com.veer.shop.pojo.User;
import com.veer.shop.pojo.ResponseData;
import com.veer.shop.service.admin.AdminUserService;
import com.veer.shop.utils.ResponseUtil;
import com.veer.shop.utils.DateTimeUtil;
import com.veer.shop.utils.EcryptUtil;
import com.veer.shop.utils.StringUtil;
import com.veer.shop.utils.ResponseResult;

@Service
public class AdminUserServiceImpl implements AdminUserService {
	
	@Autowired
	private AdminUserMapper adminUserMapper;
	
	@Override
	public ResponseData<?> update(User adminUserEntity) {
		
		// 校验参数
		Map<String, Object> result = validateAdminUserEntity(adminUserEntity);
		
		if(!(Boolean)result.get("validated")) {
			return ResponseUtil.error(ResponseCodeEnum.ERROR.getMessage() + result.get("message").toString());
		}
		
		int count = 0;
		
		adminUserEntity.setUpdatedAt(DateTimeUtil.getDateTime());
		
		if(adminUserEntity.getUserId() != null) {
			
			// 更新管理员用户
			
			// 检查管理员是否存在
			User findAdminUser = adminUserMapper.selectAdminUserById(adminUserEntity.getUserId());
			
			if(ObjectUtils.isEmpty(findAdminUser)) {
				
				// 更新的管理员不存在
				return ResponseUtil.error(Constants.ADMIN_USER_INFO_NOT_FOUND);
			}
			
			// 如果密码不为空则需要更新密码
			if(adminUserEntity.getPassword().length() > 0) {
				adminUserEntity.setPassword(EcryptUtil.generateMd5(adminUserEntity.getPassword(), findAdminUser.getSecret()));
			}
			
			count = adminUserMapper.updateAdminUser(adminUserEntity);
		} else {
			
			// 新增管理员用户
			
			// 随机密码加密密钥
			adminUserEntity.setSecret(EcryptUtil.generateUUID());
			adminUserEntity.setCreatedAt(DateTimeUtil.getDateTime());
			adminUserEntity.setPassword(EcryptUtil.generateMd5(adminUserEntity.getPassword(), adminUserEntity.getSecret()));
			adminUserEntity.setStatus(BizStatusEnum.BIZ_STATUS_AUDIT_WAITTING.getCode());
			
			count = adminUserMapper.insertAdminUser(adminUserEntity);
		}
		
		if(count > 0) {
			return ResponseUtil.success();
		}
		
		return ResponseUtil.error(ResponseCodeEnum.ADMIN_USER_CREATE_ERROR);
	}
	
	public Map<String, Object> validateAdminUserEntity(User adminUserEntity) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("validated", false); // 默认校验未通过
		
		// 校验用户名 用户名 必须 大于 5个字符
		if(adminUserEntity.getUsername().length() < Constants.PARAMS_USERNAME_VALID_LENGTH_MIN) {
			map.put("message", Constants.PARAMS_USERNAME_VALID_LENGTH_MIN_MESSAGE);
			return map;
		}
		
		// 校验密码 密码 必须 大于 5个字符
		if(adminUserEntity.getPassword().length() < Constants.PARAMS_USERNAME_VALID_LENGTH_MIN) {
			map.put("message", Constants.PARAMS_PASSWORD_VALID_LENGTH_MIN_MESSAGE);
			return map;
		}
		
		// 校验昵称 昵称 必须 大于 1个字符
		if(adminUserEntity.getName().length() < Constants.PARAMS_NAME_VALID_LENGTH_MIN) {
			map.put("message", Constants.PARAMS_NAME_VALID_LENGTH_MIN_MESSAGE);
			return map;
		}
		
		// 校验邮箱
		if(adminUserEntity.getEmail().length() > 0 && !StringUtil.validEmail(adminUserEntity.getEmail())) {
			map.put("message", Constants.PARAMS_EMAIL_VALID_MESSAGE);
			return map;
		}
		
		// 校验手机号
		if(adminUserEntity.getPhone().length() > 0 && !StringUtil.validPhone(adminUserEntity.getPhone())) {
			map.put("message", Constants.PARAMS_PHONE_VALID_MESSAGE);
			return map;
		}
		
		map.put("validated", true);
		
		return map;
	}
	
	@Override
	public ResponseData<?> getList(Map<String, String> filters) {
		// TODO Auto-generated method stub
		return null;
	}
}